<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/31
 * Time: 14:05
 */
use Swoole\Coroutine\Client;
use function Swoole\Coroutine\run;

error_reporting(0);

require  './vendor/autoload.php';

/** @var \DI\Container $container */
$container = require './bootstrap/app.php';
$config = $container->get('webSocket');


//加载数据库需要的依赖
\Asf\Database\ConnectionResolver::$container = \Asf\Routing\Route::$container = \Asf\Foundation\Http\Kernel::$container = $container;
\Asf\Foundation\Http\Kernel::initDatabase();
if (isset($argv[1])){
    $web_socket_server = $container->make(
        \Asf\Foundation\WebScoketServer::class,
        [
            'address' => $config[$argv[1]]['address'],
            'port' => $config[$argv[1]]['port']
        ]
    );
    $controller = $container->get($config[$argv[1]]['controller']);
    $web_socket_server->onOpen(function ($server, $request) use($controller) {
        $controller->onOpen($server, $request);
    });

    $web_socket_server->onMessage(function ($server, $frame) use($controller) {
        $controller->onMessage($server, $frame);
    });

    $web_socket_server->onClose(function ($server, $fd) use($controller) {
        $controller->onClose($server, $fd);
    });
    $web_socket_server->run();
}else{
    echo '缺少必要参数'.PHP_EOL;
    echo '格式完整格式为: { php webscoket.php arg1 }'.PHP_EOL;
    echo 'arg1值，为config文件definitions.php key为webSocket下的key值';
}











